﻿/*****************************************介绍*****************************************
 * 作者：User
 * 创建时间：2025-09-09 16:45:57
 * 功能：单个消除的对象
**************************************************************************************/

using DG.Tweening;
using Game.Framework;
using Spine.Unity;
using UnityEngine;
using UnityEngine.UI;

namespace Game.UI.GamePanel
{
    public class Item : MonoBehaviour
    {
        private GameObject m_Root, m_SelectAnim, m_Close, m_Hint;
        private SkeletonGraphic m_DestroyAnim;
        private Image m_Icon;
        private Button m_Btn;
        private GameDataModel.Item m_Data;
        private GamePanelView m_RootView;
        private string m_Layout;

        private void Find()
        {
            m_Root = transform.Find("Root").gameObject;
            m_DestroyAnim = transform.Find("DestroyAnim").GetComponent<SkeletonGraphic>();
            m_SelectAnim = m_Root.transform.Find("SelectAnim").gameObject;
            m_Close = m_Root.transform.Find("Close").gameObject;
            m_Icon = m_Root.transform.Find("Icon").GetComponent<Image>();
            m_Btn = m_Root.transform.Find("Border").GetComponent<Button>();
            m_Hint = m_Root.transform.Find("Hint").gameObject;
            m_Btn.onClick.AddListener(ClickBtnHandle);
        }

        private void ClickBtnHandle()
        {
            LogManager.Log($"点击格子");
            AudioManager.Instance.PlayAudio("Audios/ClickGird");
            m_RootView.SelctItem(this);
        }

        public void Init(GameDataModel.Item data, EStyle style, string layout, GamePanelView rootView)
        {
            if (!m_SelectAnim) Find();
            m_Data = data;
            m_RootView = rootView;
            m_Layout = layout;
            m_Icon.sprite = Resources.Load<Sprite>($"Imgs/Element/{style.ToString()}/{data.iconId}");

            // 适配图片尺寸
            var cellSize = (transform as RectTransform).sizeDelta;
            var finalSize = UIUtility.CalcSizeToFit(cellSize.x - 50, cellSize.y - 50, m_Icon.sprite.texture.width, m_Icon.sprite.texture.height);
            m_Icon.rectTransform.sizeDelta = new Vector2(finalSize.width, finalSize.height);

            UpdateUIStatus();
        }

        public void UpdateUIStatus()
        {
            m_Btn.gameObject.SetActive(m_Data.status != EItemStatus.Destroyed);
            m_Icon.gameObject.SetActive(m_Data.status != EItemStatus.Destroyed);
            m_Close.SetActive(m_Data.status == EItemStatus.Locked);
        }

        public void Clear()
        {
            if (!m_SelectAnim) Find();
            m_Icon.sprite = null;
            UpdateUIStatus();
            Select(false);
            DisplayDestroyAnim(false);
            m_Data = null;
            m_Root.transform.localScale = Vector3.one;
            m_Layout = "";
        }

        public void Select(bool isSelect = true)
        {
            m_SelectAnim.SetActive(isSelect);
            if (isSelect)
                m_SelectAnim.GetComponent<SkeletonGraphic>().AnimationState.SetAnimation(0, m_Layout, true);
            DisplayHint(false);
        }

        public void DisplayDestroyAnim(bool isShow = true)
        {
            m_DestroyAnim.gameObject.SetActive(isShow);
            if (isShow)
            {
                m_Root.transform.DOScale(Vector3.zero, 0.2f).OnComplete(() => UpdateUIStatus());
                m_DestroyAnim.AnimationState.SetAnimation(0, "animation", false);
            }
        }

        public void DisplayHint(bool isShow = true)
        {
            m_Hint.SetActive(isShow);
            if (isShow)
            {
                m_Hint.transform.localScale = Vector3.one;
                m_Hint.transform.DOScale(Vector3.one * .8f, 0.25f).SetLoops(-1, LoopType.Yoyo);
            }
        }

        public GameDataModel.Item Data => m_Data;
        public Image Icon => m_Icon;
    }
}